<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\tinyinfo\controller;
use app\imgsrc\model\Tinyinfo;
use think\Request;
use mip\Htmlp;
use mip\AdminApiBase;

class ApiAdminTinyinfoCode extends AdminApiBase
{

    public function do_dels(){
        $strcodes = input('post.codes');

        $res = db('tinyinfoCode')->where(array('code'=>array('in',$strcodes )) )->delete();
        $res += db('tinyinfo')->where(array('tinyinfo_code'=>array('in',$strcodes )) )->delete();
        if($res){
            return jsonSuccess('短内容分类及其下内容删除成功。');
        }else{
            return jsonError("由于未知原因，短内容分类删除失败了。");
        }
    }

    public function get_list(){

        $list = model('app\tinyinfo\model\TinyinfoCode')->getAllList();

        return jsonSuccess('查找成功',array( 'list' => $list ));

    }

    public function do_save(){
        $id = input('post.id');
        $code = input('post.code');
        $description = input('post.description');
        $saveStatus = input('post.saveStatus');

        $d1=array('code'=>$code , 'description'=>$description );

        if($saveStatus=='add'){
            db('tinyinfoCode')->insert($d1);
            return jsonSuccess('短内容分类新增成功。');
        }

        if($saveStatus=='edit'){
            $dbOldCode = db('tinyinfoCode')->where('id',$id)->find();
            $oldCode = $dbOldCode['code'];

            db('tinyinfoCode')->where('id',$id)->update($d1);

            // 更新tinycode表中的数据
            db('tinyinfo')->where('tinyinfo_code',$oldCode)->update( array('tinyinfo_code'=>$code) );

            return jsonSuccess('短内容分类编辑成功。');
        }
    }

}